#include "bsp_g3507std.h"
#include "ti_msp_dl_config.h"
#include "user_main.h"


uint32_t g_g3507_board_order_array[G3507_BOARD_ORDER_ARRAY_LEN];
uint32_t g_g3507_board_sensor_array[G3507_BOARD_SENSOR_ARRAY_LEN];

////////////////////////////////////
////////////////////////////////////
//key
key_status_t key_status;

uint16_t _get_key(void)
{
    uint32_t pin_state = 0;
    pin_state = DL_GPIO_readPins(KEY_0_PORT, KEY_0_KEY_PIN_0_PIN);
    if(pin_state & KEY_0_KEY_PIN_0_PIN)
	    return 0;
	return 1;
}

void KeyScanFSM(void)
{
    static uint16_t _mc_step;
    static uint16_t _delay_time;

    switch(_mc_step){
        case KEY_MC_IDLE:
            if(_get_key())
            {
                _delay_time = 5;
                _mc_step = KEY_MC_DEBOUNCE;
            }
            break;
        
        case KEY_MC_DEBOUNCE:
            if(_delay_time == 0)  //消抖
            {
                if(_get_key())
                {
                    _mc_step = KEY_MC_PRESS_VALID;
                }
                else
                {
                    _mc_step = KEY_MC_IDLE;
                }
            }
            else
            {
                --_delay_time;
            }
            break;
        
        case KEY_MC_PRESS_VALID:
            key_status.key_down = 1;
            _delay_time = 5;
            _mc_step = KEY_MC_WAIT_RELEASE;
            break;
        
        case KEY_MC_WAIT_RELEASE:
            if(_delay_time == 0)  
            {
                if(_get_key())
                {
                    _delay_time = 1000;
                    _mc_step = KEY_MC_LONG_PRESS_DETECTION;
                }
                else
                {
                    _mc_step = KEY_MC_RELEASE_VALID;
                }
            }
            else
            {
                --_delay_time;
            }
            break;

        case KEY_MC_RELEASE_VALID:
            key_status.key_release = 1;
            _mc_step = KEY_MC_IDLE;
            break;

        case KEY_MC_LONG_PRESS_DETECTION:
            if(_get_key())
            {
                if(_delay_time == 0)
                {
                    _mc_step = KEY_MC_LONG_PRESS_VALID;
                }
                else {
                    --_delay_time;
                }
            }
            else {
                _mc_step = KEY_MC_RELEASE_VALID;
            }
            break;

        case KEY_MC_LONG_PRESS_VALID:
            _delay_time = 500;
            key_status.key_down = 0x1 | 0xF0;
            _mc_step = KEY_MC_PRE_REPEAT;
            break;
        
        case KEY_MC_PRE_REPEAT:
            if(_get_key())
            {
                if(_delay_time == 0)
                {
                    _mc_step = KEY_MC_REPEAT;
                }
                else {
                    --_delay_time;
                }
            }
            else {
                _mc_step = KEY_MC_LONG_PRESS_RELEASE;
            }
            break;

        case KEY_MC_REPEAT:
            key_status.key_down = 1;
            _delay_time = 30;
            _mc_step = KEY_MC_PRE_REPEAT_30ms;
            break;
        
        case KEY_MC_PRE_REPEAT_30ms:
            if(_get_key())
            {
                if(_delay_time == 0)
                {
                    _mc_step = KEY_MC_REPEAT;
                }
                else {
                    --_delay_time;
                }
            }
            else {
                _mc_step = KEY_MC_LONG_PRESS_RELEASE;
            }
            break;

        case KEY_MC_LONG_PRESS_RELEASE:
            key_status.key_release = 0x01 | 0xF0;
            _mc_step = KEY_MC_IDLE;
            break;

        default:
            _mc_step = KEY_MC_IDLE;
            break;

    }
}

int g3507_board_get_sensor( uint32_t *board_sensor_array , uint32_t board_sensor_array_len  )
{
    KeyScanFSM();
    return 0;
}

int g3507_board_set_order( uint32_t *board_order_array  , uint32_t board_order_array_len  )
{

    return 0;
}
